package com.xiaozhengkeji.minecraft.plugin.fishplus.utils;

import com.xiaozhengkeji.minecraft.plugin.fishplus.config.FishConfig;
import com.xiaozhengkeji.minecraft.plugin.fishplus.entity.FishPrizeEntity;
import com.xiaozhengkeji.minecraft.plugin.fishplus.entity.FishRodEntity;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Random;

/**
 * 钓鱼核心工具集
 */
public class FishCoreUtils {
    /**
     * 通过Lore 获取指定鱼竿的配置
     */
    public static FishRodEntity getFishRodConfigByItemLore(ItemStack itemStack) {
        List<String> temp_lore = itemStack.getItemMeta().getLore();
        if (temp_lore == null) return null;
        for (String temp_key : FishConfig.FISH_ROD_ENTITY_HASH_MAP.keySet()) {
            FishRodEntity temp_fishRod = FishConfig.FISH_ROD_ENTITY_HASH_MAP.get(temp_key);
            for (String lore : temp_lore) {
                if (temp_fishRod.hasLore.equalsIgnoreCase(lore)) {
                    return temp_fishRod;
                }
            }
        }
        return null;
    }

    /**
     * 将物品存储为奖池
     */
    public static Boolean saveItemPrize(ItemStack itemStack, String prizeName) {
        String ItemJson = ItemUtils.itemToJson(itemStack);
        YamlConfiguration temp_fish_prize = YamlConfiguration.loadConfiguration(new File(FishConfig.PATH_STRING_FISH_PRIZE));
        temp_fish_prize.set(prizeName + ".PrizeType", 2);
        temp_fish_prize.set(prizeName + ".Lucky", 10);
        temp_fish_prize.set(prizeName + ".Announcement.Enable", false);
        temp_fish_prize.set(prizeName + ".Announcement.Content", "§4恭喜玩家 {p} 在钓鱼中,使用鱼竿 {rod} 获得奖品 {prize}");
        List<String> temp_prize_command = new LinkedList<>();
        temp_prize_command.add(ItemJson);
        temp_fish_prize.set(prizeName + ".Command", temp_prize_command);
        try {
            temp_fish_prize.save(FishConfig.PATH_STRING_FISH_PRIZE);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 给与一个幸运度 进行奖池判定
     */
    public static FishPrizeEntity lotteryByLucky(Integer lucky) {
        List<FishPrizeEntity> temp_prizePool = new LinkedList<>();
        for (String temp_key : FishConfig.FISH_PRIZE_ENTITY_HASH_MAP.keySet()) {
            FishPrizeEntity temp_prize = FishConfig.FISH_PRIZE_ENTITY_HASH_MAP.get(temp_key);
            if (temp_prize.lucky <= lucky) {
                temp_prizePool.add(temp_prize);
            }
        }
        if (temp_prizePool.size() == 0) {//奖品池 = 0
            Integer lowLucky = FishConfig.LuckyMax;
            for (String temp_key : FishConfig.FISH_PRIZE_ENTITY_HASH_MAP.keySet()) {
                FishPrizeEntity temp_prize = FishConfig.FISH_PRIZE_ENTITY_HASH_MAP.get(temp_key);
                if (temp_prize.lucky < lowLucky) {
                    lowLucky = temp_prize.lucky;
                }
            }
            for (String temp_key : FishConfig.FISH_PRIZE_ENTITY_HASH_MAP.keySet()) {
                FishPrizeEntity temp_prize = FishConfig.FISH_PRIZE_ENTITY_HASH_MAP.get(temp_key);
                if (temp_prize.lucky.equals(lowLucky)) {
                    return temp_prize;
                }
            }
        }
        return temp_prizePool.get(new Random().nextInt(temp_prizePool.size()) - 1);
    }

    /*发送奖品*/
    public static Boolean sendPrizes(FishPrizeEntity fishPrize, Player player) {
        for (String temp_command : fishPrize.prizeCommand) {
            if (fishPrize.prizeType == 1) {//指令
                temp_command = temp_command.replaceAll("<p>", player.getDisplayName());
                Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), temp_command);
            } else if (fishPrize.prizeType == 2) {//物品
                ItemStack temp_item = ItemUtils.jsonToItem(temp_command);
                player.getInventory().addItem(temp_item);
            }
        }
        return true;
    }
}
